cc.Class({
    extends: cc.Component,

    properties: {
        items: [cc.Node],
        txtGain: cc.Label,
        tiktokBar: cc.Node,
        motorSpts: [cc.SpriteFrame],

        lb_bar1: cc.Label,
        lb_bar2: cc.Label,
        lb_bar3: cc.Label,
        lb_bar4: cc.Label,
        lb_bar5: cc.Label,

        lb_next: cc.Label,

    },

    onLoad () {
        this.node.parent.setLocalZOrder(-1);
        var odds = GLOBAL.gameState.bikes;
        var bikes = GLOBAL.gameResult.bikes;
        var mine = [];
        GLOBAL.playerResult && (mine = GLOBAL.playerResult.list);
        var datas = [];
        var total = 0;
        for(var i = 0; i < 5; i++) datas.push({ motor: 0, bet: 0, win: 0 , odd: 0 });
        for(var x = 0; x < bikes.length; x++) {
            var b = bikes[x];
            var d = datas[b.rank - 1];
            d.motor = b.bikeId - 1;
            for(var j in mine) {
                if(b.bikeId == mine[j].bikeId) {
                    d.bet = mine[j].amount;
                    d.win = mine[j].returnAmount;
                    total += d.win;
                    break;
                }
            }
            for(var k in odds) {
                if(b.bikeId == odds[k].id) d.odd = odds[k].rate;
            }
            datas.push(d);
        }
        for(var x = 0; x < this.items.length; x++) {
            ((i) => this.initItem(i, datas[i]))(x);
        }

        this.coinTotal = total;
        this.schedule(this.updateCoinText, 0.016);

        this.initWords();
    },

    initWords: function(){
        this.lb_bar1.string = LangLocal.word12;
        this.lb_bar2.string = LangLocal.word25;
        this.lb_bar3.string = LangLocal.word8;
        this.lb_bar4.string = LangLocal.word14;
        this.lb_bar5.string = LangLocal.word18;

        this.lb_next.string = LangLocal.word42;
    },

    updateCoinText(dt) {
        var currCoin = Math.round(parseInt(this.txtGain.string) + dt / 1.2 * this.coinTotal);
        if(currCoin >= this.coinTotal) {
            currCoin = this.coinTotal;
            this.unschedule(this.updateCoinText, this);
        }
        this.txtGain.string = currCoin;
    },

    initItem(i, data) {
        var item = this.items[i];
        item.opacity = 0;
        item.x = -200;
        var spawn = cc.spawn(cc.fadeIn(0.3), cc.moveTo(0.3, 23, 0).easing(cc.easeBackOut()));
        var txtOdd = item.find('txtOdd', cc.Label);
        txtOdd.string = 'x' + data.odd.toFixed(2);
        item.find('motor_icon_a', cc.Sprite).spriteFrame = this.motorSpts[data.motor];
        item.find('motor_name', cc.RichText).string = '<color=#ffffff><outline color=black width=2><b>00' + (data.motor + 1) + '</b></outline></color>'
        if(data.bet > 0) {
            item.find('txtBet', cc.Label).string = data.bet;
            var txtWin = item.find('txtWin', cc.Label);
            txtWin.string = data.win;
            if(data.win != 0) txtWin.node.color = txtOdd.node.color = cc.hexToColor('#F14747');
            item.color = cc.hexToColor('#FFD272');
            item.runAction(cc.sequence(cc.delayTime(i * 0.05), spawn, 
                cc.scaleTo(0.3, 1.1).easing(cc.easeSineOut()), cc.scaleTo(0.2, 1).easing(cc.easeSineIn())));
        } else {
            item.find('txtBet', cc.Label).string = '';
            item.find('txtWin', cc.Label).string = '';
            item.color = cc.hexToColor('#d5d0bd');
            item.runAction(cc.sequence(cc.delayTime(i * 0.05), spawn));
        }
    },

    delayClose(t) {
        this.tiktokBar.runAction(cc.sequence(
            cc.scaleTo(t, 1),
            cc.callFunc(this.closePop, this)
        ));
    },

    closePop() {
        this.unschedule(this.updateCoinText, this);
        this.node.emit('close');
    }

});
